package com.ss.android.newmedia.e;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ss.android.newmedia.BaseApplication;
import com.ss.android.newmedia.util.InfoLRUCache;
import com.ss.android.newmedia.webview.SSWebView;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class t {
    static Map<String, Void> sInfoLRUCache = new InfoLRUCache(64, 32);
    private static t instance;
    private static Object mLock = new Object();
    private final ExecutorService mExecutorService;
    WebView mSSWebView;
    boolean b;
    int d;
    Handler mHandler;

    private t() {
        this.mExecutorService = Executors.newSingleThreadExecutor();
        this.mHandler = new Handler();
    }

    public static t inst() {
        if (t.instance == null) {
            synchronized (mLock) {
                if (t.instance == null) {
                    t.instance = new t();
                }
            }
        }
        return t.instance;
    }

    public static boolean a(String arg2) {
        return (android.support.design.a.isTextEmpty(arg2)) || (t.sInfoLRUCache.containsKey(Uri.parse(arg2).getPath()));
    }

    static void a(t arg2) {
        if (!arg2.b) {
            return;
        }

        synchronized (arg2) {
            try {
                arg2.wait(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void b() {
        t.sInfoLRUCache.clear();
    }

    public final void a(String arg5, Map url) {
        new a(this, url)
                .executeOnExecutor(this.mExecutorService, new String[]{arg5});
    }

    final class a extends AsyncTask<String, Void, String> {
        private final t b;
        private Map<String, String> additionalHttpHeaders;

        public a(t arg1, Map<String, String> headers) {
            this.b = arg1;
            this.additionalHttpHeaders = headers;
        }

        @Override
        protected void onPostExecute(String url) {
            t v0 = this.b;
            if (v0.mSSWebView == null) {
                v0.mSSWebView = new SSWebView(BaseApplication.getApplication());
                v0.mSSWebView.setWebViewClient(new WebViewClient() {

                    public final void onPageFinished(WebView webView, String url) {
                        super.onPageFinished(webView, url);
                        if (!url.equals("about:blank")) {
                            webView.loadUrl("about:blank");
                            t.this.b = false;
                            synchronized (t.this) {
                                t.this.notify();
                            }
                            ++t.this.d;
                            t.sInfoLRUCache.put(Uri.parse(url).getPath(), null);
                        }
                    }

                    public final void onPageStarted(WebView webView, String url, Bitmap favicon) {
                        super.onPageStarted(webView, url, favicon);
                        if (!url.equals("about:blank")) {
                            t.this.b = true;
                            t.this.mHandler.postDelayed(new Runnable() {

                                public final void run() {
                                    int v0 = t.this.d;
                                    int v1 = t.this.d;
                                    if (v0 == v1) {
                                        t.this.mSSWebView.loadUrl("about:blank");
                                        t.this.b = false;
                                        synchronized (t.this) {
                                            t.this.notify();
                                        }
                                    }
                                }
                            }, 5000L);
                        }
                    }

                    public final void onReceivedError(WebView webView, WebResourceRequest
                            webResourceRequest, WebResourceError webResourceError) {
                        super.onReceivedError(webView, webResourceRequest, webResourceError);
                        t.this.b = false;
                    }
                });
                v0.mSSWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            }

            if (!android.support.design.a.isTextEmpty(url) && this.b.mSSWebView != null) {
                this.b.mSSWebView.loadUrl(url, this.additionalHttpHeaders);
            }
        }

        @Override
        protected String doInBackground(String... params) {
            t.a(this.b);
            String v0 = t.a(params[0]) ? null : params[0];
            return v0;
        }
    }
}

